package com.macro.admin.service.impl;

import com.macro.admin.pojo.Admin;
import com.macro.admin.pojo.Menu;
import com.macro.admin.dao.MenuDao;
import com.macro.admin.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author Xiao Hai
 * @since 2021-04-19
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuDao, Menu> implements MenuService {

    @Resource
    private MenuDao menuDao;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 通过用户id查询菜单列表
     * @return
     *//*
    @Override
    public List<Menu> getMenusByAdminId() {
        List<Menu> menus;
        //获取用户id Integer 转成string类型
        String id = Integer.toString(((Admin)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId());
        //创建redis集合对象
        ListOperations forList = redisTemplate.opsForList();
        //判断redis里是否有用户id的key
        if (redisTemplate.hasKey("adminId_"+id)){
            //有者查询
            menus = forList.range("adminId_"+id, 0, -1);
        }else {
            //如果没有就到数据库里查询
            //获取((Admin)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId()全局id
            menus = menuDao.getMenusByAdminId(((Admin)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId());
            //放入redis中
            forList.leftPushAll("adminId_"+id,menus);
            //设置过期时间
            redisTemplate.expire("adminId_"+id,30,TimeUnit.MINUTES);
            //在从redis中查询
            menus = forList.range("adminId_"+id, 0, -1);
        }
        return menus;

    }*/

    /**
     * 通过用户id查询菜单列表
     * @return
     */
    @Override
    public List<Menu> getMenusByAdminId() {
        //全局获取用户id
        //获取((Admin)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId()全局id
        Integer aminId = (((Admin)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId());
        //创建redis对象
        ValueOperations opsForValue = redisTemplate.opsForValue();
        //通过key拿到对象
        List<Menu> menus = (List<Menu>) opsForValue.get("menu_" + aminId);
        //如果为空，去数据库获取，并放入redis中
        if (CollectionUtils.isEmpty(menus)){
            //如果没有就到数据库里查询
            List<Menu> menuList = menuDao.getMenusByAdminId(aminId);
            opsForValue.set("menu_" + aminId,menuList,30,TimeUnit.MINUTES);
            menus = (List<Menu>) opsForValue.get("menu_" + aminId);
        }
        return menus;

    }

    /**
     * 根据角色获取菜单列表
     * @return
     */
    @Override
    public List<Menu> getMenusWithRole() {
        return menuDao.getMenusWithRole();
    }
}
